﻿''' <summary>
''' 代理キー列属性
''' </summary>
''' <remarks>
''' 代理キー呼び出しを行うための列。主キーではありません。
''' ユニーク制約と同等。
''' </remarks>
Public Class AlternativeKeyAttribute
    Inherits ColumnAttribute

#Region "Constructor"
    Public Sub New()
        'Load関数を差し替える
        Me.AlternativeKeyLoadWhereCommandBuilder = AddressOf Me.CreateLoadWhereCommand

        'Save関数を差し替える
        Me.SaveWhereCommandCreater = AddressOf Me.CreateSaveWhereCommand
    End Sub

    Public Sub New(ByVal ColumnName As String)
        Me.New()
        Me.ColumnName = ColumnName
    End Sub
#End Region

    Private Sub CreateLoadWhereCommand(value As Object, sqlBuilder As Action(Of String), paramRegister As Func(Of Object, String))
        Dim pname As String = paramRegister.Invoke(value)
        sqlBuilder.Invoke(String.Format("{0} = {1}", Me.ColumnName, pname))
    End Sub

    Private Sub CreateSaveWhereCommand(instance As Object, archiveValue As Object, sqlBuilder As Action(Of String), paramRegister As Func(Of Object, String))
        Dim pname As String = paramRegister.Invoke(archiveValue)
        sqlBuilder.Invoke(String.Format("{0} = {1}", Me.ColumnName, pname))
    End Sub

End Class
